public class MaxRotateFunction {

    public int maxRotateFunction1(int[] nums) {
        int index = 0;
        int ans = Integer.MIN_VALUE;
        int n = nums.length;
        while (index <= n - 1) {
            int sum = 0;
            int temp = index;
            for (int i = 0; i < n; i++) {
                sum += nums[i] * (index % n);
                ++index;
            }
            ans = Math.max(ans, sum);
            index = temp + 1;
        }
        return ans;
    }

    public int maxRotateFunction2(int[] nums) {
        int n = nums.length;
        if (n == 1) return 0;
        int l = 1;
        int r = n - 1;
        int ans = 0;
        int sum = nums[0];
        for (int i = 1; i < n; i++) {
            ans += nums[i] * i;
            sum += nums[i];
        }
        int res = ans;
        for (; l < n; ++l, --r) {
            ans += sum;
            ans -= nums[r] * n;
            res = Math.max(res, ans);
        }
        return res;
    }

    public int maxRotateFunction(int[] nums) {
        int sum = 0;
        int res = 0;
        int n = nums.length;
        if (n == 1) return 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            res += nums[i] * i;
        }
        int ans = res;
        for (int i = n - 1; i > 0; i--) {
            res = res + sum - nums[i] * n;
            ans = Math.max(ans, res);
        }
        return ans;
    }
}
